package com.ww.core.widget;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.LocalActivityManager;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.Html;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ww.core.R;
import com.ww.core.util.SlideBg;

/**
 * 动画页签使用方法:/br>
 * 1、必须继承此 AnimTabActivity</br>
 * 2、必须调用setTitle(int or String);</br>
 * 3、必须调用setIndicator(String title, int tab, Intent intent)</br>
 * 4、必须设�? intTabWidth()---></br>
 * @version 2013-7-29
 * @author wangwei
 */
public class AnimTabActivity extends Activity {
	public int type = -1;
	public static final String ANIM = "anim";
	public static ImageButton right;
	public LinearLayout linearLayout1, view;
	public Context context = AnimTabActivity.this;
	LocalActivityManager manager = null;
	public ViewPager pager = null;
	private ProgressDialog mProgressDialog;//发送请求 等待的dialog

	public int currentTabID = 0;
	public TextView tvFront;
	public RelativeLayout layout;
	public int avgWidth = 0;
	public int startX = 0;
	public ArrayList<View> list = new ArrayList<View>();
	int w;

	public void findView() {
		setContentView(R.layout.core_slidetab);
		manager = new LocalActivityManager(this, true);
		manager.dispatchCreate(getIntent().getExtras());
		pager = (ViewPager) findViewById(R.id.viewpage);
		right = (ImageButton) findViewById(R.id.right);
		linearLayout1 = (LinearLayout) findViewById(R.id.linearLayout1);
		view = (LinearLayout) findViewById(R.id.view);

		layout = (RelativeLayout) findViewById(R.id.layout_title_bar);
		tvFront = new TextView(context);
		tvFront.setGravity(Gravity.CENTER);
		tvFront.setBackgroundColor(getResources().getColor(R.color.slide_bg));

		WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		w = wm.getDefaultDisplay().getWidth();
		pager.setOnPageChangeListener(new MyOnPageChangeListener());
	}

	public void addView(int layout) {
		View v = LayoutInflater.from(context).inflate(layout, null);
		view.addView(v);
	}

	public void setIndicator(String title, int tab, Intent intent) {
		TextView localView = new TextView(context);
		localView.setText(title);
		localView.setTextColor(getResources().getColor(R.color.animtab_noselected));
		localView.setBackgroundResource(R.drawable.mylist_item);
		localView.setGravity(Gravity.CENTER);
		localView.setTextSize(16);
		localView.setOnClickListener(new MyOnClickListener(tab));
		localView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT,
				1.0f));
		linearLayout1.addView(localView);

		list.add(getView(tab + "", intent));
	}

	public void removeAllView() {
		pager.removeAllViews();
		linearLayout1.removeAllViews();
		layout.removeAllViews();
	}

	public void setBackground(int background) {
		pager.setBackgroundResource(background);
	}

	/**
	 * ��ʼ������
	 */
	public void initTabWidth() {
		if (linearLayout1.getChildCount() > 0) {
			avgWidth = w / linearLayout1.getChildCount();
			tvFront.setWidth(avgWidth);
			layout.addView(tvFront);
			((TextView) linearLayout1.getChildAt(0)).setTextColor(getResources().getColor(R.color.animtab_selected));
			pager.setAdapter(new MyPagerAdapter(list));
			pager.setCurrentItem(0);
		}

	}

	/**
	 * 设置右上角按钮图标.
	 * @param image 资源
	 */
	public void setRightButton(int image) {
		try {
			right.setImageResource(image);
			right.setVisibility(View.VISIBLE);
		} catch (Exception e) {
		}
	}

	/**
	 * 创建进度框.
	 * @param mContext Context
	 */
	public void showProgressDialog(Context mContext) {
		if (mProgressDialog == null) {
			ProgressDialog dialog = new ProgressDialog(mContext);
			dialog.setIndeterminate(true);
			dialog.setCancelable(true);
			mProgressDialog = dialog;
		}
		try {
			mProgressDialog.show();
			mProgressDialog.setContentView(R.layout.core_loading);
		} catch (Exception e) {
		}
	}

	public void hideProgressDialog() {
		if (mProgressDialog != null) {
			mProgressDialog.dismiss();
		}
	}

	/**
	 * 设置右上角按钮图标.
	 * @param image 资源
	 */
	public void setRightText(String text) {
		try {
			TextView right_txt = (TextView) findViewById(R.id.right_txt);
			right_txt.setText(text);
			right_txt.setVisibility(View.VISIBLE);
			right = (ImageButton) findViewById(R.id.right);
			right.setVisibility(View.VISIBLE);
		} catch (Exception e) {
		}
	}

	/**
	 * 初始化皮肤.
	 * @param title 标题栏文字
	 */
	public void setTitle(String title) {
		findView();
		initTitle(title);
	}

	public void initTitle(String title) {
		try {
			if (title != null) {
				TextView textView = (TextView) findViewById(R.id.title);
				textView.setText(title);
			}
		} catch (Exception e) {
		}

		try {
			Button back = (Button) findViewById(R.id.back);
			back.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					finish();
				}
			});
		} catch (Exception e) {
		}
	}

	/**
	 * 初始化皮肤.
	 * @param title 标题栏文字
	 */
	public void setTitle(int title) {
		setTitle(getResources().getString(title));
	}


	/**
	 * 公共手机返回按钮事件.
	 * @param keyCode keyCode
	 * @param event  KeyEvent
	 * @return  boolean
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}

	public void showMessage(String message) {
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.core_toast, (ViewGroup) findViewById(R.id.toast_layout_root));
		TextView textView = (TextView) view.findViewById(R.id.text);
		textView.setText(Html.fromHtml(message));
		Toast toast = new Toast(context);
		toast.setDuration(Toast.LENGTH_LONG);
		toast.setView(view);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.show();
	}

	public void showMessage(int message) {
		showMessage(getResources().getString(message));
	}

	/**
	 * ͨ��activity��ȡ��ͼ
	 * @param id
	 * @param intent
	 * @return
	 */
	public View getView(String id, Intent intent) {
		return manager.startActivity(id, intent).getDecorView();
	}

	/**
	 * Pager������
	 */
	private class MyPagerAdapter extends PagerAdapter {
		List<View> list = new ArrayList<View>();

		public MyPagerAdapter(ArrayList<View> list) {
			this.list = list;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			ViewPager pViewPager = ((ViewPager) container);
			pViewPager.removeView(list.get(position));
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			ViewPager pViewPager = ((ViewPager) arg0);
			pViewPager.addView(list.get(arg1));
			return list.get(arg1);
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {

		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {
		}
	}

	/**
	 * ҳ���л�����
	 */
	private class MyOnPageChangeListener implements OnPageChangeListener {
		@Override
		public void onPageSelected(int arg0) {
			changeTab(arg0);
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {

		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {

		}
	}

	/**
	 * 页签切换.
	 * @param arg0 滑动后页签
	 */
	public void changeTab(int arg0) {
		SlideBg.moveFrontBg(tvFront, startX, avgWidth * arg0, 0, 0);
		startX = avgWidth * arg0;

		if (arg0 <= currentTabID) {
			Log.e("arg0 <= currentTabID", currentTabID + "");
			if (currentTabID <= 0) {
				currentTabID = 0;
			} else {
				currentTabID = pager.getCurrentItem() - 1;
			}
		} else if (arg0 >= currentTabID) {//�������󻬶�
			Log.e("arg0 >= currentTabID", currentTabID + "");
			if (currentTabID >= pager.getChildCount() - 1) {
				currentTabID = pager.getChildCount() - 1;
			} else {
				currentTabID = pager.getCurrentItem() + 1;
			}
		}

		for (int i = 0; i < linearLayout1.getChildCount(); i++) {
			TextView text = (TextView) linearLayout1.getChildAt(i);
			if (i == arg0) {
				text.setTextColor(getResources().getColor(R.color.animtab_selected));
				text.setTextSize(18);
			} else {
				text.setTextColor(getResources().getColor(R.color.animtab_noselected));
				text.setTextSize(16);
			}
		}
	}

	/**
	 * ͷ��������
	 */
	public class MyOnClickListener implements View.OnClickListener {
		private int index = 0;

		public MyOnClickListener(int i) {
			index = i;
		}

		@Override
		public void onClick(View v) {
			pager.setCurrentItem(index);
		}
	};
}
